<?PHP
defined('_XR_EXEC') or die();
/**
 * 控制器处理类 
 */
class account_order_Controller extends controller_user1{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','我的订单');
		
		$this->assign('account_menu1','3');  
	}
	##################################
	/* 我的订单：begin */
	################################## 
	function index__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';
			if(!isset($_POST['types']))   $_POST['types']  = '';
  	  #查询条件
			//$sql_where = ' where 0=0 '.func::speedsearch($_POST['keywords'],array( 'a.remark','a.fee','b.name','c.username','c.name' ));
			$sql_where = ' where a.user_id="'.factory::module('member')->getuserid().'" ';
			
			if($_POST['types'] != ''){
				switch($_POST['types']){
					case "1"://等待审核
						$sql_where .= ' and a.status=1 ';
						break;
					case "2"://即将发货
						$sql_where .= ' and a.status in (2,3) ';
						break;
					case "3"://已经发货
						$sql_where .= ' and a.status=4 ';
						break;
					default://取消的订单
						$sql_where .= ' and a.status in (-1,-2) ';
						break;
				}
			}
			
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_order` a
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*,
					b.val as sendtime
				from `t_order` a
				left join t_list_sendtime b on a.sendtime=b.id
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				foreach($rs as &$v){
					$v['order_goods'] = $db->loadassoclist('select * from t_order_goods where order_id="'.$v['id'].'" ');
					foreach($v['order_goods'] as &$val ){
						$val['order_goods_item'] = $db->loadassoclist('select a.*,b.name as vege_name from t_order_goods_item a left join t_vege b on a.vege_id=b.id where a.order_id="'.$v['id'].'" and a.order_goods_id="'.$val['id'].'" ');
					}
				}
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}
		$this->assign('account_menu2','1'); 
		
		 
	} 
	##################################
	/* end */
	################################## 
	/* 订单数量统计 */
	function orderstatusnum__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 
			$db = factory::getDBO();
			$user_id=factory::module('member')->getuserid(); 
			# 有效订单（除去系统取消、用户取消的订单）
			//$this->assign('ordernum_ok',$db->loadresult('select count(*) from t_order where user_id="'.$user_id.'" and status not in (-2,-1) '));
			 
			# 全部订单
			$allorder = $db->loadresult('select count(*) from t_order where user_id="'.$user_id.'" ');   
			# 等待审核
			$waitsh = $db->loadresult('select count(*) from t_order where user_id="'.$user_id.'" and status=1  ');   
			# 正在备货
			$waitsend = $db->loadresult('select count(*) from t_order where user_id="'.$user_id.'" and status in (2,3)  ');
			# 已经发货
			$send = $db->loadresult('select count(*) from t_order where user_id="'.$user_id.'" and status=4  ');  
			#　取消的订单
			$cancel = $db->loadresult('select count(*) from t_order where user_id="'.$user_id.'" and status  in (-2,-1)  '); 
			exit('{success:1,allorder:"'.$allorder.'",waitsh:"'.$waitsh.'",waitsend:"'.$waitsend.'",send:"'.$send.'",cancel:"'.$cancel.'"}');
		}
		exit();	
	}
	
	##################################
	/* 取消订单：begin */
	################################## 
	function cancelorder__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			$id = $_POST['id'];
			$user_id=factory::module('member')->getuserid();
			$m_order  = factory::module('order');
			if(!$m_order->cancelByUser($id,$user_id)){
				exit('操作失败 '.$m_order->getError());
			}
			exit('{success:1}');
		}
		exit();
	} 
	##################################
	/* end */
	################################## 
	 
	 
	 
	 
	 
}
?>